home *** CD-ROM | disk | FTP | other *** search
/ AP Professional Graphics CD-ROM Library / AP Professional Graphics CD-ROM Library.iso / pc / unix / appendix / gemsiii / grphgems.h < prev    next >
Encoding:
C/C++ Source or Header  |  1993-08-25  |  4.2 KB  |  158 lines

  1. /* 
  2.  * GraphicsGems.h  
  3.  * Version 1.0 - Andrew Glassner
  4.  * from "Graphics Gems", Academic Press, 1990
  5.  */
  6.  
  7. #ifndef GG_H
  8.  
  9. #define GG_H 1
  10.  
  11. /*********************/
  12. /* 2d geometry types */
  13. /*********************/
  14.  
  15. typedef struct Point2Struct {   /* 2d point */
  16.         double x, y;
  17.         } Point2;
  18. typedef Point2 Vector2;
  19.  
  20. typedef struct IntPoint2Struct {        /* 2d integer point */
  21.         int x, y;
  22.         } IntPoint2;
  23.  
  24. typedef struct Matrix3Struct {  /* 3-by-3 matrix */
  25.         double element[3][3];
  26.         } Matrix3;
  27.  
  28. typedef struct Box2dStruct {            /* 2d box */
  29.         Point2 min, max;
  30.         } Box2;
  31.         
  32.  
  33. /*********************/
  34. /* 3d geometry types */
  35. /*********************/
  36.  
  37. typedef struct Point3Struct {   /* 3d point */
  38.         double x, y, z;
  39.         } Point3;
  40. typedef Point3 Vector3;
  41.  
  42. typedef struct IntPoint3Struct {        /* 3d integer point */
  43.         int x, y, z;
  44.         } IntPoint3;
  45.  
  46.  
  47. typedef struct Matrix4Struct {  /* 4-by-4 matrix */
  48.         double element[4][4];
  49.         } Matrix4;
  50.  
  51. typedef struct Box3dStruct {            /* 3d box */
  52.         Point3 min, max;
  53.         } Box3;
  54.  
  55.  
  56.  
  57. /***********************/
  58. /* one-argument macros */
  59. /***********************/
  60.  
  61. /* absolute value of a */
  62. #define ABS(a)          (((a)<0) ? -(a) : (a))
  63.  
  64. /* round a to nearest int */
  65. #define ROUND(a)    floor((a)+0.5)
  66.  
  67. /* take sign of a, either -1, 0, or 1 */
  68. #define ZSGN(a)         (((a)<0) ? -1 : (a)>0 ? 1 : 0)  
  69.  
  70. /* take binary sign of a, either -1, or 1 if >= 0 */
  71. #define SGN(a)          (((a)<0) ? -1 : 1)
  72.  
  73. /* shout if something that should be true isn't */
  74. #define ASSERT(x) \
  75. if (!(x)) fprintf(stderr," Assert failed: x\n");
  76.  
  77. /* square a */
  78. #define SQR(a)          ((a)*(a))       
  79.  
  80.  
  81. /***********************/
  82. /* two-argument macros */
  83. /***********************/
  84.  
  85. /* find minimum of a and b */
  86. #define MIN(a,b)        (((a)<(b))?(a):(b))     
  87.  
  88. /* find maximum of a and b */
  89. #define MAX(a,b)        (((a)>(b))?(a):(b))     
  90.  
  91. /* swap a and b (see Gem by Wyvill) */
  92. #define SWAP(a,b)       { a^=b; b^=a; a^=b; }
  93.  
  94. /* linear interpolation from l (when a=0) to h (when a=1)*/
  95. /* (equal to (a*h)+((1-a)*l) */
  96. #define LERP(a,l,h)     ((l)+(((h)-(l))*(a)))
  97.  
  98. /* clamp the input to the specified range */
  99. #define CLAMP(v,l,h)    ((v)<(l) ? (l) : (v) > (h) ? (h) : v)
  100.  
  101.  
  102. /****************************/
  103. /* memory allocation macros */
  104. /****************************/
  105.  
  106. /* create a new instance of a structure (see Gem by Hultquist) */
  107. #define NEWSTRUCT(x)    (struct x *)(malloc((unsigned)sizeof(struct x)))
  108.  
  109. /* create a new instance of a type */
  110. #define NEWTYPE(x)      (x *)(malloc((unsigned)sizeof(x)))
  111.  
  112.  
  113. /********************/
  114. /* useful constants */
  115. /********************/
  116.  
  117. #define PI              3.141592        /* the venerable pi */
  118. #define PITIMES2        6.283185        /* 2 * pi */
  119. #define PIOVER2         1.570796        /* pi / 2 */
  120. #define E               2.718282        /* the venerable e */
  121. #define SQRT2           1.414214        /* sqrt(2) */
  122. #define SQRT3           1.732051        /* sqrt(3) */
  123. #define GOLDEN          1.618034        /* the golden ratio */
  124. #define DTOR            0.017453        /* convert degrees to radians */
  125. #define RTOD            57.29578        /* convert radians to degrees */
  126.  
  127.  
  128. /************/
  129. /* booleans */
  130. /************/
  131.  
  132. #define TRUE            1
  133. #define FALSE           0
  134. #define ON              1
  135. #define OFF             0
  136. typedef int boolean;                    /* boolean data type */
  137. typedef boolean flag;                   /* flag data type */
  138.  
  139. extern double V2SquaredLength(), V2Length();
  140. extern double V2Dot(), V2DistanceBetween2Points(); 
  141. extern Vector2 *V2Negate(), *V2Normalize(), *V2Scale(), *V2Add(), *V2Sub();
  142. extern Vector2 *V2Lerp(), *V2Combine(), *V2Mul(), *V2MakePerpendicular();
  143. extern Vector2 *V2New(), *V2Duplicate();
  144. extern Point2 *V2MulPointByProjMatrix();
  145. extern Matrix3 *V2MatMul(), *TransposeMatrix3();
  146.  
  147. extern double V3SquaredLength(), V3Length();
  148. extern double V3Dot(), V3DistanceBetween2Points();
  149. extern Vector3 *V3Normalize(), *V3Scale(), *V3Add(), *V3Sub();
  150. extern Vector3 *V3Lerp(), *V3Combine(), *V3Mul(), *V3Cross();
  151. extern Vector3 *V3New(), *V3Duplicate();
  152. extern Point3 *V3MulPointByMatrix(), *V3MulPointByProjMatrix();
  153. extern Matrix4 *V3MatMul();
  154.  
  155. extern double RegulaFalsi(), NewtonRaphson(), findroot();
  156.  
  157. #endif
  158.